$(function(){
	initPage();
});

function initPage() {
	initExecuteSelect();
	getExecuteProperties();
}

function initExecuteSelect(){
	$.ajax({
	url : "/smarttest-frontstage/messagedrainage/initSelect.action",
	dataType : 'json',
	data : {},
	method : 'post',
	async:false,
	success : function(data) {
		if (data.status == 200 && data.data) {
			var html = "";
			var temp = data.data;
			for (i in temp) {
				var singleProtol = temp[i];
				html += "<option value=\""+singleProtol+"\">"
						+ singleProtol
						+ "</option>"
			}
			$("#executeobjects").empty();
			$("#executeobjects").html(html);
		}
	}
  });
}

function getExecuteProperties(){
	url = $("#executeobjects").val();
	$.ajax({
		url : "/smarttest-frontstage/messagedrainage/getExecuteStatus.action",
		dataType : 'json',
		data : {url : url},
		method : 'post',
		async:false,
		success : function(data) {
			if (data.status == 200 && data.data) {
				if(data.data.runningflag==true){
				$("#destination").val(data.data.destination);
				$("#imeifilter").val(data.data.imeisDefault);
				$("#cmdfilter").val(data.data.cmdsDefault);
				pageAddCaseControl(true,true,true,true,false);
				}else{
				pageAddCaseControl(false,false,false,false,true);
				}
			}
		}
	  });
}

function pageAddCaseControl(val1, val2, val3, val4, val5) {
	if (val1 == true) {
		$("#destination").attr("disabled", "disabled");
	} else {
		$("#destination").removeAttr("disabled");
	}
	if (val2 == true) {
		$("#imeifilter").attr("disabled", "disabled");
	} else {
		$("#imeifilter").removeAttr("disabled");
	}
	if (val3 == true) {
		$("#cmdfilter").attr("disabled", "disabled");
	} else {
		$("#cmdfilter").removeAttr("disabled");
	}
	if (val4 == true) {
		$("#startdrainage").attr("disabled", "disabled");
	} else {
		$("#startdrainage").removeAttr("disabled");
	}
	if (val5 == true) {
		$("#stopdrainage").attr("disabled", "disabled");
	} else {
		$("#stopdrainage").removeAttr("disabled");
	}
}

function startdrainage(){
	var url = $("#executeobjects").val();
	var destination = $("#destination").val();
	var imeifilter = $("#imeifilter").val();
	var cmdfilter = $("#cmdfilter").val();
	$.ajax({
		url : "/smarttest-frontstage/messagedrainage/start.action",
		dataType : 'json',
		data : {url : url,
			destination : destination,
			imeifilter : imeifilter,
			cmdfilter : cmdfilter},
		method : 'post',
		async:false,
		success : function(data) {
			if (data.status == 200 && data.data) {
				alertDialog('启动环境', '操作成功');
				pageAddCaseControl(true,true,true,true,false);
			}
		}
	});
}

function stopdrainage(){
	url = $("#executeobjects").val();
	$.ajax({
		url : "/smarttest-frontstage/messagedrainage/stop.action",
		dataType : 'json',
		data : {url : url},
		method : 'post',
		async:false,
		success : function(data) {
			if (data.status == 200 && data.data) {
				alertDialog('停止发送', '操作成功');
				pageAddCaseControl(false,false,false,false,true);
			}
		}
	});
}

function alertDialog(title, content) {
	$.SmartMessageBox({
		title : "<i class='fa fa-sign-out txt-color-orangeDark'></i>" + title
				+ "<span class='txt-color-orangeDark'><strong>"
				+ $('#show-shortcut').text() + "</strong></span>",
		content : content,
		buttons : '[确定]'
	}, function(ButtonPressed) {
		if (ButtonPressed == "确定") {
		}
	});
}